package com.haidao.admin.component.web.job;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.haidao.admin.component.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@DisallowConcurrentExecution
public class ReportJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String tableName = jobExecutionContext.getJobDetail().getJobDataMap().getString("code");
        long taskId = jobExecutionContext.getJobDetail().getJobDataMap().getLong("taskId");
        log.info("start execute job {} with id {}", tableName, taskId);
        // 数据库名称
        if (StrUtil.isEmpty(tableName)) {
            throw new JobExecutionException("上报任务执行失败，未指定上报表名");
        }
        ReportService bean = SpringUtil.getBean(tableName, ReportService.class);
        bean.report(tableName, taskId);
    }
}